翻訳と辞書
Words near each other
・ "O" Is for Outlaw
・ "O"-Jung.Ban.Hap.
・ "Ode-to-Napoleon" hexachord
・ "Oh Yeah!" Live
・ "Our Contemporary" regional art exhibition (Leningrad, 1975)
・ "P" Is for Peril
・ "Pimpernel" Smith
・ "Polish death camp" controversy
・ "Pro knigi" ("About books")
・ "Prosopa" Greek Television Awards
・ "Pussy Cats" Starring the Walkmen
・ "Q" Is for Quarry
・ "R" Is for Ricochet
・ "R" The King (2016 film)
・ "Rags" Ragland
・ ! (album)
・ ! (disambiguation)
・ !!
・ !!!
・ !!! (album)
・ !!Destroy-Oh-Boy!!
・ !Action Pact!
・ !Arriba! La Pachanga
・ !Hero
・ !Hero (album)
・ !Kung language
・ !Oka Tokat
・ !PAUS3
・ !T.O.O.H.!
・ !Women Art Revolution


Dictionary Lists
翻訳と辞書 辞書検索 [ 開発暫定版 ]
スポンサード リンク

default constructor : ウィキペディア英語版
default constructor
In computer programming languages the term default constructor can refer to a constructor that is automatically generated by the compiler in the absence of any programmer-defined constructors (e.g. in Java), and is usually a nullary constructor. In other languages (e.g. in C++) it is a constructor that can be called without having to provide any arguments, irrespective of whether the constructor is auto-generated or user-defined. Note that a constructor with formal parameters can still be called without arguments if default arguments were provided in the constructor's definition.
== C++ ==

In C++, the standard describes the default constructor for a class as a constructor that can be called with no arguments (this includes a constructor whose parameters all have default arguments).〔C++ standard, ISO/IEC 14882:1998, 12.1.5
C++ standard, ISO/IEC 14882:2003, 12.1.5〕 For example:

class MyClass
;
MyClass :: MyClass() : x(100) // constructor defined
int main()

When allocating memory dynamically, the constructor may be called by adding parenthesis after the class name. In a sense, this is an explicit call to the constructor:

int main()

If the constructor does have one or more parameters, but they all have default values, then it is still a default constructor. Remember that each class can have at most one default constructor, either one without parameters, or one whose all parameters have default values, such as in this case:

class MyClass
;
MyClass :: MyClass(int i, std::string s) // constructor defined

In C++, default constructors are significant because they are automatically invoked in certain circumstances; and therefore, in these circumstances, it is an error for a class to not have a default constructor:
* When an object value is declared with no argument list (e.g.: MyClass x;) or allocated dynamically with no argument list (e.g.: new MyClass; or new MyClass();), the default constructor of MyClass is used to initialize the object.
* When an array of objects is declared, e.g. MyClass x();; or allocated dynamically, e.g. new MyClass (). The default constructor of MyClass is used to initialize all the elements.
* When a derived class constructor does not explicitly call the base class constructor in its initializer list, the default constructor for the base class is called.
* When a class constructor does not explicitly call the constructor of one of its object-valued fields in its initializer list, the default constructor for the field's class is called.
* In the standard library, certain containers "fill in" values using the default constructor when the value is not given explicitly. E.g. vector(10); initializes the vector with ten elements, which are filled with a default-constructed MyClass object.
If a class has no explicitly defined constructors, the compiler will implicitly declare and define a default constructor for it. This implicitly defined default constructor is equivalent to an explicitly defined one with an empty body. For example:〔Computer Science A Structured Approach Using C++ by Behrouz A. Forouzan and Richard F. Gilberg〕

class MyClass
;
int main()

If constructors are explicitly defined for a class, but they are all non-default, the compiler will not implicitly define a default constructor, leading to a situation where the class does not have a default constructor. This is the reason for a typical error, demonstrated by the following example.

class MyClass
;
MyClass :: MyClass (int y)
int main()

Since neither the programmer nor the compiler has defined a default constructor, the creation of the objected pointed to by p leads to an error.〔Computer Science A Structured Approach Using C++ by Behrouz A. Forouzan and Richard F. Gilberg〕
On the other hand in C++11 a default constructor can be explicitly created:

class MyClass
;

Or explicitly inhibited:

class MyClass
;


抄文引用元・出典: フリー百科事典『 ウィキペディア(Wikipedia)
ウィキペディアで「default constructor」の詳細全文を読む



スポンサード リンク
翻訳と辞書 : 翻訳のためのインターネットリソース

Copyright(C) kotoba.ne.jp 1997-2016. All Rights Reserved.